import { Scene_MenuBase } from "@/scenes/Scene_MenuBase.js";
import { Window_EquipStatus } from "@/windows/Window_EquipStatus.js";
import { Window_EquipItem } from "@/windows/Window_EquipItem.js";
import { Window_EquipCommand } from "@/windows/Window_EquipCommand.js";
import { Window_EquipSlot } from "@/windows/Window_EquipSlot.js";
import { config } from "@/managers/ConfigManager";
import { TextManager } from "@/managers/TextManager";
import { SceneManager } from "@/managers/SceneManager";

export class Scene_Equip extends Scene_MenuBase {
    constructor() {
        super();
    }

    create() {
        super.create();
        this.createHelpWindow();
        this.createStatusWindow();
        this.createCommandWindow();
        this.createSlotWindow();
        this.createItemWindow();

        this.attachHandler();
    }

    start(){
        super.start();
        this.refreshActor();
    }

    createStatusWindow() {
        this._statusWindow = new Window_EquipStatus(312,288);
        this._statusWindow.y=this._helpWindow.height;
        this.addWindow(this._statusWindow);
    }

    createCommandWindow() {
        let wx = this._statusWindow.width;
        let wy = this._helpWindow.height;
        let ww = config.width - wx;
        this._commandWindow = new Window_EquipCommand(ww,72);
        this._commandWindow.x=wx;
        this._commandWindow.y=wy;
        this.addWindow(this._commandWindow);
    }

    createSlotWindow() {
        let wx = this._statusWindow.width;
        let wy = this._commandWindow.y + this._commandWindow.height;
        let ww = config.width - wx;
        let wh = this._statusWindow.height - this._commandWindow.height;
        this._slotWindow = new Window_EquipSlot(ww,wh);
        this._slotWindow.x=wx;
        this._slotWindow.y=wy;
        this._slotWindow.deactivate();
        this.addWindow(this._slotWindow);
    }

    createItemWindow() {
        let wx = 0;
        let wy = this._statusWindow.y + this._statusWindow.height;
        let ww = config.width;
        let wh = config.height - wy;
        this._itemWindow = new Window_EquipItem(ww,wh);
        this._itemWindow.x=wx;
        this._itemWindow.y=wy
        this._itemWindow.deactivate();
        this.addWindow(this._itemWindow);
    }

    attachHandler(){
        this._commandWindow.setHandler(TextManager.equip2,()=>{
            this._slotWindow.activate();
            this._commandWindow.deactivate();
        });
        this._commandWindow.setHandler(TextManager.optimize,()=>{
            this.actor().optimizeEquipments();
            this._statusWindow.refresh();
            this._slotWindow.refresh();
        });
        this._commandWindow.setHandler(TextManager.clear,()=>{
            this.actor().clearEquipments();
            this._statusWindow.refresh();
            this._slotWindow.refresh();
        });
        this._commandWindow.setHandler('cancel',()=>{
            SceneManager.pop();
        });

        this._slotWindow.setHandler('ok',(etypeId)=>{
            this._itemWindow.activate();
            this._slotWindow.deactivate();
            this._itemWindow.select(0);
        });
        this._slotWindow.setHandler('changed',(etypeId,oid)=>{
            this._itemWindow.setETypeId(etypeId);
        });
        this._slotWindow.setHandler('cancel',()=>{
            this._slotWindow.deactivate();
            this._commandWindow.activate();
        });
        
        this._itemWindow.setHandler('ok',(equip,index)=>{
            this._actor.changeEquip(index,equip);
            this._slotWindow.refreshItem(index);
            this._statusWindow.refresh();
            this._itemWindow.deactivate();
            this._slotWindow.activate();
            this._itemWindow.select(-1);
        });
        this._itemWindow.setHandler('changed',(equip,index,oeqp,oi)=>{
            this._statusWindow.reDraw(equip,index);
            this._helpWindow.setItem(equip);
        });
        this._itemWindow.setHandler('cancel',()=>{
            this._itemWindow.deactivate();
            this._slotWindow.activate();
            this._itemWindow.select(-1);
        });

    }

    refreshActor() {
        let actor = this.actor();
        this._statusWindow.setActor(actor);
        this._slotWindow.setActor(actor);
        this._itemWindow.setActor(actor);
    }
}